﻿using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;

namespace Nirvana
{
    [ExecuteInEditMode]
    [RequireComponent(typeof(RawImage))]
    public class UIRawImageLoadByURL : MonoBehaviour
    {
        [SerializeField]
        [Tooltip("The raw image url.")]
        private string url;
        [SerializeField]
        private bool autoFitNativeSize;
        [SerializeField]
        private bool autoUpdateAspectRatio;
        [SerializeField]
        private bool autoDisable = true;
        private RawImage _rawimage;
        private UnityWebRequest _webrequest;

        public bool AutoFitNativeSize
        {
            get
            {
                return this.autoFitNativeSize;
            }
            set
            {
                this.autoFitNativeSize = value;
            }
        }
        public bool AutoUpdateAspectRatio
        {
            get
            {
                return this.autoUpdateAspectRatio;
            }
            set
            {
                this.autoUpdateAspectRatio = value;
            }
        }
        public bool AutoDisable
        {
            get
            {
                return this.autoDisable;
            }
            set
            {
                this.autoDisable = value;
            }
        }

        public string URL
        {
            get
            {
                return this.url;
            }
            set
            {
                if (!this.url.Equals(value))
                {
                    this.url = value;
                    if (!string.IsNullOrEmpty(this.url))
                    {
                        this.LoadRawImage(this.url);
                    }
                }
            }
        }


        private void Awake()
        {
            this._rawimage = base.GetComponent<RawImage>();
        }

        private void OnEnable()
        {
            if (this._rawimage.texture == null)
            {
                if (!string.IsNullOrEmpty(this.url))
                {
                    this.LoadRawImage(this.url);
                }
                else if (this.autoDisable)
                {
                    this._rawimage.enabled = false;
                }
            }
        }

        private void OnDestroy()
        {
            if (this._rawimage != null)
            {
                this._rawimage.texture = null;
            }
        }

        private void LoadRawImage(string url)
        {
            if (url.Contains("://"))
            {
                Scheduler.RunCoroutine(this.DownRawImage(this.url));
            }
            else
            {
                byte[] array = File.ReadAllBytes(url);
                TextureFormat textureFormat = TextureFormat.RGB24;
                if (array != null && array[1] == 80 && array[2] == 78 && array[3] == 71)
                {
                    textureFormat = TextureFormat.RGBA32;
                }
                Texture2D texture2D = new Texture2D(2, 2, textureFormat, false);
                if (!ImageConversion.LoadImage(texture2D, array, true))
                {
                    this._rawimage.texture = null;
                    if (this.autoDisable)
                    {
                        this._rawimage.enabled = false;
                    }
                    return;
                }
                texture2D.wrapMode = TextureWrapMode.Clamp;
                this._rawimage.texture = texture2D;
                if (this.autoDisable)
                {
                    this._rawimage.enabled = true;
                }
                if (this.autoFitNativeSize)
                {
                    this._rawimage.SetNativeSize();
                }
                if (this.autoUpdateAspectRatio)
                {
                    AspectRatioFitter component = this._rawimage.GetComponent<AspectRatioFitter>();
                    if (component != null)
                    {
                        component.aspectRatio = (float)texture2D.width / (float)texture2D.height;
                    }
                }
            }
        }

        private IEnumerator DownRawImage(string url)
        {
            UnityWebRequest texture = UnityWebRequestTexture.GetTexture(url, true);
            yield return texture.SendWebRequest();
            if (texture.isNetworkError)
            {
                Debug.LogError("Load image from url failed: " + texture.error);
                yield break;
            }
            if (this == null)
            {
                yield break;
            }
            if (url != this.url)
            {
                yield break;
            }
            Texture2D content = DownloadHandlerTexture.GetContent(texture);
            texture.Dispose();
            if (content != null)
            {
                this._rawimage.texture = content;
                if (this.autoDisable)
                {
                    this._rawimage.enabled = true;
                }
                if (this.autoFitNativeSize)
                {
                    this._rawimage.SetNativeSize();
                }
                if (this.autoUpdateAspectRatio)
                {
                    AspectRatioFitter component = this._rawimage.GetComponent<AspectRatioFitter>();
                    if (component != null)
                    {
                        component.aspectRatio = (float)content.width / (float)content.height;
                    }
                }
            }
            else
            {
                this._rawimage.texture = null;
                if (this.autoDisable)
                {
                    this._rawimage.enabled = false;
                }
            }
            yield break;
        }

    }
}

